package com.jwthhealth_pub.community.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.community.view.CommunityActivity;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;

/**
 * Created by liuzhen on 2017/12/17.
 */

public class CommunityDocTagAdapter extends RecyclerView.Adapter {

    private final String TAG = makeLogTag(CommunityDocTagAdapter.class);

    private final CommunityActivity mActivity;
    private final String[] mDocTags;

    private int selPos;

    public CommunityDocTagAdapter(CommunityActivity communityActivity, String[] docTags) {
        this.mActivity = communityActivity;
        this.mDocTags = docTags;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(View.inflate(mActivity, R.layout.item_commnuity, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder vh = (ViewHolder) holder;

        vh.tagName.setText(mDocTags[position]);

        Log.d(TAG, "selPos:" + selPos);

        if (position == selPos)
            vh.rb.setBackgroundResource(R.drawable.bg_border_radius_primary);
        else
            vh.rb.setBackgroundResource(R.drawable.bg_border_radius_primary_black);


        vh.rb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "position(click):" + position);

                selPos = position;
                mActivity.refreshDecTags();
                mActivity.showDocs();
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDocTags.length;
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        ImageView rb;
        TextView tagName;

        public ViewHolder(View itemView) {
            super(itemView);

            rb = itemView.findViewById(R.id.rb_community_docstags);
            tagName = itemView.findViewById(R.id.tv_community_doc_tag);
        }
    }
}
